package com.ktg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.ktgcommon.domain.wms.WmProductProduceLine;
import com.ktg.ktgcommon.vo.WeekDayDate;
import com.ktg.mapper.wmProductSalseLineMapper;
import com.ktg.service.wmProductSalseLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class wmProductSalseLineServiceImpl implements wmProductSalseLineService {
    @Autowired
    private wmProductSalseLineMapper mapper;

    //查询从七天之前到现在之内的数据，然后进行数据计算。返回WeekDayDate
    @Override
    public List<WeekDayDate> checkProduct() {
        List<WmProductProduceLine> list = mapper.checkProduct();
        //得到的日期数据list
        List<WeekDayDate> weekDayDateList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
             WeekDayDate weekDayDate=new WeekDayDate();
             weekDayDate.setDayOfMonth(list.get(i).getQuantitySalse());
             weekDayDate.setDayOfWeek(list.get(i).getCreateTime().toLocalDate());
             weekDayDateList.add(weekDayDate);
        }
        // 使用 Map 来聚合相同日期的数量
        Map<LocalDate, Double> aggregated = new HashMap<>();

        for (WeekDayDate item : weekDayDateList) {
            LocalDate date = item.getDayOfWeek();
            Double amount = item.getDayOfMonth();
            aggregated.merge(date, amount, Double::sum);
        }
        //得到的结果的日期数据list
        List<WeekDayDate> dayDateList=new ArrayList<>();
        dayDateList= aggregated.entrySet().stream()
                .map(entry -> new WeekDayDate(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
        return dayDateList;
    }

    @Override
    public Page<WmProductProduceLine> checkPage(Page<WmProductProduceLine> page,
                                                LocalDate startDate,
                                                LocalDate endDate, String quantitySalse) {
        Page<WmProductProduceLine> page1=  mapper.checkPage(page,startDate,endDate,quantitySalse);
        return page1;
    }
}
